function usergroup()
{
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=usergroup&tmpl=raw";
	var callback = function(){
		if($("#grid_usergroup").length > 0)
		{
			var dataSource = $("#grid_usergroup").data("kendoGrid").dataSource;
			dataSource.read();
		}
		
	}
	obj.callback = callback;
	return obj;
}

function showModalEditUSergroup(id)
{
	var obj = {};
	var link = "index.php?option=com_ketnoi&view=usergroup&layout=create&tmpl=raw&quickcreate=1&id="+id;
	obj.link = link;
	var callback = function(){
		if($("#form_create_usergroup").length > 0)
		{
			initCreateUsergroup(false,false,true);
		}
		
	}
	obj.callback = callback;
	showModalLink(obj);
}

function deleteUserGroup(id)
{
	alertify.confirm("Bạn muốn xóa nhóm này?", function (e) {
	    if (e) {
	    	showLoader();
			var link = "index.php?option=com_ketnoi&view=usergroup&task=usergroup.delete&tmpl=raw&id="+id;
			$.get(link,"",function(response){
				var dataSource = $("#grid_usergroup").data("kendoGrid").dataSource;
				dataSource.read();
				hideLoader();		
				alertify.set({ delay: 4000 });
				alertify.success("Xóa thành công");
			})
	    } 
	});
}

function showModalCreateUserGroup_popup()
{
	var obj = {};
	var link = "index.php?option=com_ketnoi&view=usergroup&layout=create&tmpl=raw&quickcreate=1";
	obj.link = link;
	var callback = function(){
		if($("#form_create_usergroup").length > 0)
		{
			initCreateUsergroup(false,false);
		}
		
	}
	obj.callback = callback;
	showModalLink(obj);
}

function showModalCreateUserGroup(client_update)
{
	var obj = {};
	var link = "index.php?option=com_ketnoi&view=usergroup&layout=create&tmpl=raw&quickcreate=1&client_update="+client_update;
	obj.link = link;
	var callback = function(){
		if($("#form_create_usergroup").length > 0)
		{
			initCreateUsergroup(true);
		}
		
	}
	obj.callback = callback;
	showModalLink(obj);
}

function initCreateUsergroup(update,firstpage,isEdit)
{
	firstpage = firstpage || true;
	isEdit = isEdit || false;
	$("#form_create_usergroup").validate({  
		errorPlacement: function(error, element) {
			if($(element).hasClass("error"))
			{
				hideLoader($('#form_create_usergroup'));
			}
            error.remove();          
        },
        onkeyup:false,
        onfocusout:false,
        rules:{
            title:{
                required:true,
                remote:{
        			url:"index.php?option=com_ketnoi&task=usergroup.checkTenExists&tmpl=raw",
        			data:{
        				tennhom:function(value){       	
        					return $("#form_create_usergroup input[name='title']").val();
        				},
        				oldval:$("#oldten").val()
        			},
        			beforeSend:function(){
        				showLoader($('#form_create_usergroup'));
        			}
        		}
            }
        },
        messages:{
            title:{
                required:'Vui lòng nhập tên nhóm',    
                remote:'Tên nhóm này đã tồn tại.Vui lòng nhập tên khác.'
            }
        }
    })
    
    
    
    $('#form_create_usergroup').ajaxForm({
    	beforeSubmit:function(){
    		showLoader($('#form_create_usergroup'));
    	},
    	success:function(response){
    		var isQuickCreate = $('#form_create_usergroup input[name="quickcreate"]').val() == 1?true:false;
    		if(isQuickCreate)
			{
    			var result = JSON.parse(response);
    			if(result.success)
    			{
    				if(update)
    				{
	    				var client_update = $('#form_create_usergroup input[name="client_update"]').val();
	    				var option = "<option value='"+result.id+"'>"+result.title+"</option>";
	    				$("#"+client_update).append(option);
	    				$("#"+client_update).trigger("liszt:updated");
    				}else
    				{
    					var dataSource = $("#grid_usergroup").data("kendoGrid").dataSource;
    					if(firstpage)
    						dataSource.page(1);
    					else dataSource.read();
    				}
    				$('#form_create_usergroup').closest("div.modal").find("div.modal-header button.close").trigger("click");
    				alertify.set({ delay: 4000 });
    				if(isEdit)
    					alertify.success("Sửa thành công");
    				else alertify.success("Thêm nhóm thành công");
    			}
    			
			}
    		hideLoader($('#form_create_usergroup'));
    	}
    });
}



